package com.alibaba.algo.apiimpl.feign;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.AppFeignService;
import com.alibaba.algo.cooperation.feign.vo.enums.FunctionCodeEnum;
import com.alibaba.algo.cooperation.feign.vo.res.AppInfoResVO;
import com.alibaba.algo.domain.app.AppInfoDomain;
import com.alibaba.algo.entity.app.AppInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
public class AppFeignServiceImpl implements AppFeignService {

    @Autowired
    private AppInfoDomain appInfoDomain;

    /**
     * 获取全量应用列表
     *
     * @return 用户列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult<List<AppInfoResVO>> qryAllAppList() {
        List<AppInfo> appInfoList = appInfoDomain.listByPara(new AppInfo(), null);
        List<AppInfoResVO> resVOList = new ArrayList<>();
        appInfoList.forEach(s -> {
            AppInfoResVO resVO = new AppInfoResVO();
            resVO.setAppId(s.getId());
            resVO.setAppCode(s.getAppCode());
            resVO.setAppName(s.getAppName());
            resVOList.add(resVO);
        });
        return BaseResult.buildSuccess(resVOList);
    }

    /**
     * 根据功能编码获取拥有该功能的应用
     *
     * @param functionCodeEnum 功能编码
     * @return 应用列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult<List<AppInfoResVO>> qryAllAppList(FunctionCodeEnum functionCodeEnum) {
        if (ObjectUtils.isEmpty(functionCodeEnum)) {
            return BaseResult.buildSuccess(Collections.emptyList());
        }
        String functionCode = functionCodeEnum.getCode();

        return null;
    }
}
